package com.bc.ljn.catwars;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;

/**
 * Created by ljn on 2014/12/23.
 */
public class Player {
    //分数当前帧下标
    private int scoreIndex;
    //主角的血量与血量位图
    //默认三格
    private int playerHP = 3;
    private Bitmap bmpPlayerHP;
    //主角的坐标以及位图
    public int x, y;
    private Bitmap bmpPlayer;
    //主角的移动速度
    private int speed = 5;
    //主角移动标识
    private boolean isPressed;
    //碰撞后处于无敌时间
    //计时器
    private int noCollisionCount = 0;
    //因为无敌时间
    private int noCollisionTime = 60;
    //是否碰撞的标识位
    private boolean isCollision;
    //player和触摸屏的相对位置
    int relativeX = 0;
    int relativeY = 0;

    int pointX;
    int pointY;


    //主角的构造函数
    public Player(Bitmap bmpPlayer, Bitmap bmpPlayerHP) {
        this.bmpPlayer = bmpPlayer;
        this.bmpPlayerHP = bmpPlayerHP;
        x = GameView.screenW / 2 - bmpPlayer.getWidth() / 2;
        y = GameView.screenH - bmpPlayer.getHeight();
    }

    // 主角的绘制函数
    public void draw(Canvas canvas, Paint paint) {
        //绘制主角
        //当处于无敌时间时，绘制一次主角
        if (isCollision) {
            //每2次游戏循环，绘制一次主角
            if (noCollisionCount % 2 == 0) {
                canvas.drawBitmap(bmpPlayer, x, y, paint);
            }
        } else {
            canvas.drawBitmap(bmpPlayer, x, y, paint);
        }
        //绘制主角血量
        for (int i = 0; i < playerHP; i++) {
            canvas.drawBitmap(bmpPlayerHP,
                    i * bmpPlayerHP.getWidth(), 5, paint);
        }
    }

    public void onTouchEvent(MotionEvent event) {
        //获取用户当前触屏位置
        pointX = (int) event.getX();
        pointY = (int) event.getY();

        if (event.getAction() == MotionEvent.ACTION_DOWN) { //当用户是按下动作
            relativeX = pointX - x;
            relativeY = pointY - y;
            isPressed = true;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) { //当用户是移动动作

        } else if (event.getAction() == MotionEvent.ACTION_UP) { // 当用户是抬起动作
            isPressed = false;
        }
    }

    public void logic() {
        //处理主角移动
        if (isPressed) {
            x = pointX - relativeX;
            y = pointY - relativeY;
        }

        //判断屏幕X边界
        if (x + bmpPlayer.getWidth() >= GameView.screenW) {
            x = GameView.screenW - bmpPlayer.getWidth();
        } else if (x <= 0) {
            x = 0;
        }

        //判断屏幕Y边界
        if (y + bmpPlayer.getHeight() >= GameView.screenH) {
            y = GameView.screenH - bmpPlayer.getHeight();
        } else if (y <= 0) {
            y = 0;
        }

        //处理无敌状态
        if (isCollision) {
            //计时器开始
            noCollisionCount++;
            if (noCollisionCount >= noCollisionTime) {
                //无敌时间过后，接触无敌状态及初始化计数器
                isCollision = false;
                noCollisionCount = 0;
            }
        }
    }

    // 设置主角血量
    public void setPlayerHP(int hp) {
        this.playerHP = hp;
    }

    // 获取主角血量
    public int getPlayerHP() {
        return playerHP;
    }

    //判断碰撞
    public boolean isCollsionWith(Enemy en) {
        //是否处于无敌时间
        if (isCollision == false) {
            int x2 = en.x;
            int y2 = en.y;
            int w2 = en.frameW;
            int h2 = en.frameH;

            if (x >= x2 && x >= x2 + w2) {
                return false;
            } else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
                return false;
            } else if (y >= y2 && y >= y2 + h2) {
                return false;
            } else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
                return false;
            }
            //碰撞即进入无敌状态
            isCollision = true;
            return true;
            //处于无敌状态，无视碰撞
        } else {
            return false;
        }
    }

    //判断碰撞（主角与敌机子弹）
    public boolean isCollisionWith(Bullet bullet) {
        //是否处于无敌时间
        if (isCollision == false) {
            int x2 = bullet.bulletX;
            int y2 = bullet.bulletY;
            int w2 = bullet.bmpBullet.getWidth();
            int h2 = bullet.bmpBullet.getHeight();
            if (x >= x2 && x >= x2 + w2) {
                return false;
            } else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
                return false;
            } else if (y >= y2 && y >= y2 + h2) {
                return false;
            } else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
                return false;
            }
            //碰撞即进入无敌状态
            isCollision = true;
            return true;
            //处于无敌状态，无视碰撞
        } else {
            return false;
        }
    }
}
